﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lidgren.Network;

namespace Model.LiveControl
{
    public  class CoustomMouseEventArgs: ISerializable
    {
        public double RatioX;
        public double RatioY;
        public int BtnFlag;
        public int Delta;
        public int Clicks;

        public CoustomMouseEventArgs(double ratioX, double ratioY, MouseEventArgs mouseEventArgs)
        {
            RatioX = ratioX;
            RatioY = ratioY;
            Delta = mouseEventArgs.Delta;
            Clicks = mouseEventArgs.Clicks;
            _btn = mouseEventArgs.Button;
            BtnFlag = 0;
        }

        public int FlagUp
        {
            get
            {
                int flag = 0;
                switch (_btn)
                {
                    case MouseButtons.Left:
                        flag = MouseEventFlag.MOUSEEVENTF_LEFTUP;
                        break;
                    case MouseButtons.None:
                        break;
                    case MouseButtons.Right:
                        flag = MouseEventFlag.MOUSEEVENTF_RIGHTUP;
                        break;
                    case MouseButtons.Middle:
                        flag = MouseEventFlag.MOUSEEVENTF_MIDDLEUP;
                        break;
                    case MouseButtons.XButton1:
                        break;
                    case MouseButtons.XButton2:
                        break;
                    default:
                        break;
                }
                return flag;
            }
        }

        public bool IsLeft => _btn == MouseButtons.Left;

        public int FlagDown
        {
            get
            {
                int flag = 0;
                switch (_btn)
                {
                    case MouseButtons.Left:
                        flag = MouseEventFlag.MOUSEEVENTF_LEFTDOWN;
                        break;
                    case MouseButtons.None:
                        break;
                    case MouseButtons.Right:
                        flag = MouseEventFlag.MOUSEEVENTF_RIGHTDOWN;
                        break;
                    case MouseButtons.Middle:
                        flag = MouseEventFlag.MOUSEEVENTF_MIDDLEDOWN;
                        break;
                    case MouseButtons.XButton1:
                        break;
                    case MouseButtons.XButton2:
                        break;
                    default:
                        break;
                }
                return flag;
            }
        }
        private readonly MouseButtons _btn;

        public CoustomMouseEventArgs()
        {
        }

        public CoustomMouseEventArgs(System.Windows.Forms.MouseEventArgs mouseEventArgs, int screenWidth, int screenHeight)
        {
            //统一转换为标准坐标。
            RatioX = (double) mouseEventArgs.X/(double) screenWidth;
            RatioY = (double) mouseEventArgs.Y/(double) screenHeight;
            Delta = mouseEventArgs.Delta;
            Clicks = mouseEventArgs.Clicks;
            _btn = mouseEventArgs.Button;
            BtnFlag = 0;
        }
        public void WritePayload(NetOutgoingMessage message)
        {
            message.Write(RatioX);
            message.Write(RatioY);
            message.Write(BtnFlag);
            message.Write(Delta);
            message.Write(Clicks);

        }

        public void ReadPayload(NetIncomingMessage message)
        {
            RatioX = message.ReadDouble();
            RatioY = message.ReadDouble();
            BtnFlag = message.ReadInt32();
            Delta = message.ReadInt32();
            Clicks = message.ReadInt32();
        }
    }
}
